package wujinye.com.controller.meet;

import wujinye.com.controller.BaseServlet;
import wujinye.com.service.MeetService;
import wujinye.com.service.impl.MeetServiceImpl;
import wujinye.com.utils.StrUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/meet/*")
public class MeetServlet extends BaseServlet {

    private MeetService meetService = new MeetServiceImpl();

    /**
     * 请求meet.jsp的接口
     *
     * @param req
     * @param resp
     */
    public void meeting(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String search = req.getParameter("search");
            String deptName = req.getParameter("deptName");
            String currentPage = req.getParameter("page");
            int page = 1;
            if (StrUtil.checkString(currentPage)) {
                page = StrUtil.stringToInteger(currentPage);
            }
            if (StrUtil.checkString(search)) {
                String encode = URLEncoder.encode(search, "UTF-8");
                if (StrUtil.checkString(deptName)) {
                    String encode1 = URLEncoder.encode(deptName, "UTF-8");
                    resp.sendRedirect(req.getContextPath() + "/view/showMeetList?page=" + page + "&search=" + encode + "&deptName=" + encode1);
                } else {
                    resp.sendRedirect(req.getContextPath() + "/view/showMeetList?page=" + page + "&search=" + encode);
                }
            } else {
                if (StrUtil.checkString(deptName)) {
                    String encode1 = URLEncoder.encode(deptName, "UTF-8");
                    resp.sendRedirect(req.getContextPath() + "/view/showMeetList?page=" + page + "&deptName=" + encode1);
                } else {
                    resp.sendRedirect(req.getContextPath() + "/view/showMeetList?page=" + page);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 取消会议请求
     *
     * @param req
     * @param resp
     */
    public void cancelMeeting(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String mId = req.getParameter("mId");
            if (StrUtil.checkString(mId)) {
                meetService.cancelMeeting(StrUtil.stringToInteger(mId));
                resp.getWriter().write("success");
            } else {
                resp.getWriter().write("取消失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 确认参加会议请求
     *
     * @param req
     * @param resp
     */
    public void joinMeeting(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String mId = req.getParameter("mId");
            String userId = req.getSession().getAttribute("userId").toString();
            if (StrUtil.checkString(mId) && StrUtil.checkString(userId)) {
                meetService.joinMeeting(StrUtil.stringToInteger(mId), StrUtil.stringToInteger(userId));
                resp.getWriter().write("success");
            } else {
                resp.getWriter().write("确认参加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发布会议请求
     *
     * @param req
     * @param resp
     */
    public void addMeeting(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String deptId = req.getParameter("deptId");
            String title = req.getParameter("title");
            String startTime = req.getParameter("startTime");
            String content = req.getParameter("content");
            String userId = req.getSession().getAttribute("userId").toString();
            if (StrUtil.checkString(deptId) && StrUtil.checkString(title) && StrUtil.checkString(startTime)) {
                meetService.addMeeting(StrUtil.stringToInteger(userId), deptId, title, content, startTime);
                resp.sendRedirect(req.getContextPath()+"/view/showMeetList");
                return;
            } else {
                req.getSession().setAttribute("meetingmsg", "标题，部门，开始时间不能为空！！！");
                resp.sendRedirect(req.getContextPath() + "/view/writeMeeting");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
